var app = getApp();

var pageData = {
    data: {
        goods_box:[],
        showResult: false, //搜索结果展示
        itemStyle: 1, //商品列表显示方式 单列显示 1  双列显示 2
        res_data:[], //储存数据
        tab: 9,
        direction: 0,
        prevPage: 0,
        pageData: {},
        itemsList: [],
        history: [],
        inputContent: '',
        filterParam: {
            form: 'goods',
            id:0,
            page: 1,
            page_size: 10,
            sort_key: '',
            sort_direction: '',
            is_integral: '',
            idx_arr: {
                idx: '',
                idx_value: ''
            },
            search_value: '',
            region_id: ''
        },
        animationData: {},
        filter: false,
        locationList: [],
        DataUrl:'',
        DataObject:{},
        SelectFlag:0, //选择 状态
        GoodsAllMoney:0
    },
    onLoad: function (e) {

    },
    onShow: function () {  //生命周期函数--监听页面显示  每次跳转到此页面 就会调用
      // if (app.globalData.UserInfo.Phone == undefined) {
      //   app.turnToPage('/default/pages/bindCellphone/bindCellphone');
      // }
      this.userShopping();

    },
    userShopping(){   //用户购物车
        var url = app.globalData.HOST + app.APILIST.getmyshopping;
        var data = {
            "data":{
            "userid":[app.globalData.UserInfo.id],
            "limit": app.globalData.goods_limit,
             }
        }
        if (app.globalData.UserInfo.id == undefined){
  
          app.turnToPage('/default/pages/bindCellphone/bindCellphone');
        }else{
            app.WxRequest(url,data,1,
                (res)=>{
                  // console.log(res)
                  this.data.res_data = res.data;
                  this.data.res_data.forEach((it)=>{
                    it.checked = false;
                    it.discount = (it.goods.price * it.goods_num) * app.globalData.UserInfo.rate;
                    it.price = it.goods.price * it.goods_num;
                    it.discount = it.discount.toFixed(2);
                    it.price = it.price.toFixed(2);
                  })

            this.setData({
                pageData: res
            })
            this.setData({
                itemsList: res.data
            })
        },( ref )=>{
        });
    }
    },
    getMoreItems: function (e) {  //下拉 事件
        let curPage = e.target.dataset.curpage;

        if (curPage < this.data.pageData.last_page) {
            this.data.filterParam.page = this.data.filterParam.page + 1;
            this.setData({prevPage: curPage});
            var url = app.globalData.HOST + app.APILIST.getmyshopping;
            var data =   {
                "data":{
                   "userid":[app.globalData.UserInfo.id],
                   "limit": app.globalData.goods_limit,
               }
            }
            app.WxRequest(url, data, this.data.filterParam.page,
                (res) =>{
                var list = [...this.data.itemsList,...res.data];
                this.data.res_data = list;
            this.setData({
                pageData: res
            })
            this.setData({
                itemsList: list
            })
        },( ref )=>{
                console.log(ref);
            });
        };
    },
    checkboxChange: function (e) {  //复选框狂点击事件
      this.data.res_data.forEach((it) => {
        if (it.id == e.currentTarget.dataset.id) {
            it.checked=!it.checked;
          }
      })
      this.setData({
        itemsList: this.data.res_data
      })
      this.jiesuan();
    },
    jian_shopping(e){  //减商品数量
      var rate = app.globalData.UserInfo.rate

      if (e.currentTarget.dataset.sum.goods_num>1){
        var goodsSum = e.currentTarget.dataset.sum.goods_num - 1;
        this.data.res_data.forEach((it) => {
          if (it.id == e.currentTarget.dataset.sum.id) {
            it.goods_num = goodsSum;
            it.discount = it.goods.price * goodsSum * rate;
            it.price = it.goods.price * goodsSum;
            it.discount = it.discount.toFixed(2);
            it.price = it.price.toFixed(2);
          }
        })
        this.setData({
          itemsList: this.data.res_data
        })
      }
    },
    jia_shopping(e) {  //加商品数量

      var rate = app.globalData.UserInfo.rate
      if (e.currentTarget.dataset.sum.goods_num <= e.currentTarget.dataset.sum.goods.stock){
        var goodsSum = e.currentTarget.dataset.sum.goods_num + 1;
        this.data.res_data.forEach((it) => {
          if (it.id == e.currentTarget.dataset.sum.id) {
            it.goods_num = goodsSum;
            it.discount = it.goods.price * goodsSum * rate;
            it.price = it.goods.price * goodsSum;
            it.discount = it.discount.toFixed(2);
            it.price = it.price.toFixed(2);
          }
        })

        this.setData({
          itemsList: this.data.res_data
        })
      }
    },
    addToCart: function (e) {  //删除购物车
        console.log(e.currentTarget.dataset.id)
      var url = app.globalData.HOST + app.APILIST.delshopping;
      var data = {
          "data": {
                  "shoppingid": [e.currentTarget.dataset.id]
          }
       };
      app.WxRequest(url, data, 1,
        (res) => {
          if(res.status_code == 200){
            wx.showToast({
                title: '删除成功',
                image: '/images/dui.png',
                duration: 1000
            });
            this.userShopping();
        };
    }, (ref) => {

        });
    },
    checkboxChange_Select(){ //全选
      var Sflag;
      if (this.data.SelectFlag == 0){
        Sflag = true;
        this.data.SelectFlag = 1;
        console.log('true');
      }else{
        Sflag = false;
        console.log('false');
        this.data.SelectFlag = 0;
      }
      this.data.res_data.forEach((it) => {
        it.checked = Sflag;
      })
      this.setData({
        itemsList: this.data.res_data
      })
      this.jiesuan();
    },
    jiesuan(){  //结算 商品金额
      var goods_money = 0;
      this.data.res_data.forEach((it) => {
        if (it.checked == true){
          goods_money += it.discount*1;
        }
      })
      this.setData({
        GoodsAllMoney: goods_money
      })
    },
    purchase(){  //生成订单  
    

      var Arr =[];
      var obje  = {};
      this.data.res_data.forEach((item)=>{
        if (item.checked == true){
          obje = {
            'goodsid': item.goods.id,
            'num': item.goods_num
          }
            Arr.push(obje);
        }
      });
      var url = app.globalData.HOST + app.APILIST.createorders;
      var data = {
        "userid": app.globalData.UserInfo.id,
        "data":Arr
      };
      app.WxRequest(url,data, 1,
        (res) => {
          if (res) {
            switch (res.status_code) {
              case 200:
                app.globalData.OrderList = res.data;
                app.turnToPage('/pages/page_order/page_order');
                break;
              case 500:
                wx.showToast({
                  title: res.Massage,
                  image: '/images/jinggao.png',
                  duration: 2000
                })
                break;
            }
          }
        }, (ref) => {

        });
    },
    dataInitial: function () {
        app.pageDataInitial();
    },
    /**
 * 跳转到详情页
 */
    turnToDetail: function (e) {

      // let id = e.currentTarget.dataset.id;
      // app.turnToPage('/eCommerce/pages/goodsDetail/goodsDetail?goodsid=' + id);

    },
    onShareAppMessage: function (e) {
        return app.onPageShareAppMessage(e);
    },


};
Page(pageData);
